/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package entityprojet;

import entityprojet.exceptions.NonexistentEntityException;
import entityprojet.exceptions.PreexistingEntityException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

/**
 *
 * @author Ron
 */
public class FDocenteteJpaController implements Serializable {

    public FDocenteteJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public FDocenteteJpaController() {
        emf = Persistence.createEntityManagerFactory("projetT_RPU");
    }
    
    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(FDocentete FDocentete) throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            FCaisse cbCANo = FDocentete.getCbCANo();
            if (cbCANo != null) {
                cbCANo = em.getReference(cbCANo.getClass(), cbCANo.getCbMarq());
                FDocentete.setCbCANo(cbCANo);
            }
            FCollaborateur cbCONoCaissier = FDocentete.getCbCONoCaissier();
            if (cbCONoCaissier != null) {
                cbCONoCaissier = em.getReference(cbCONoCaissier.getClass(), cbCONoCaissier.getCbMarq());
                FDocentete.setCbCONoCaissier(cbCONoCaissier);
            }
            FCollaborateur cbCONo = FDocentete.getCbCONo();
            if (cbCONo != null) {
                cbCONo = em.getReference(cbCONo.getClass(), cbCONo.getCbMarq());
                FDocentete.setCbCONo(cbCONo);
            }
            FCompteg CGNum = FDocentete.getCGNum();
            if (CGNum != null) {
                CGNum = em.getReference(CGNum.getClass(), CGNum.getCbMarq());
                FDocentete.setCGNum(CGNum);
            }
            FComptet CTNumPayeur = FDocentete.getCTNumPayeur();
            if (CTNumPayeur != null) {
                CTNumPayeur = em.getReference(CTNumPayeur.getClass(), CTNumPayeur.getCbMarq());
                FDocentete.setCTNumPayeur(CTNumPayeur);
            }
            FComptet CTNumCentrale = FDocentete.getCTNumCentrale();
            if (CTNumCentrale != null) {
                CTNumCentrale = em.getReference(CTNumCentrale.getClass(), CTNumCentrale.getCbMarq());
                FDocentete.setCTNumCentrale(CTNumCentrale);
            }
            FDepot cbDENo = FDocentete.getCbDENo();
            if (cbDENo != null) {
                cbDENo = em.getReference(cbDENo.getClass(), cbDENo.getCbMarq());
                FDocentete.setCbDENo(cbDENo);
            }
            FLivraison cbLINo = FDocentete.getCbLINo();
            if (cbLINo != null) {
                cbLINo = em.getReference(cbLINo.getClass(), cbLINo.getCbMarq());
                FDocentete.setCbLINo(cbLINo);
            }
            em.persist(FDocentete);
            if (cbCANo != null) {
                cbCANo.getFDocenteteCollection().add(FDocentete);
                cbCANo = em.merge(cbCANo);
            }
            if (cbCONoCaissier != null) {
                cbCONoCaissier.getFDocenteteCollection().add(FDocentete);
                cbCONoCaissier = em.merge(cbCONoCaissier);
            }
            if (cbCONo != null) {
                cbCONo.getFDocenteteCollection().add(FDocentete);
                cbCONo = em.merge(cbCONo);
            }
            if (CGNum != null) {
                CGNum.getFDocenteteCollection().add(FDocentete);
                CGNum = em.merge(CGNum);
            }
            if (CTNumPayeur != null) {
                CTNumPayeur.getFDocenteteCollection().add(FDocentete);
                CTNumPayeur = em.merge(CTNumPayeur);
            }
            if (CTNumCentrale != null) {
                CTNumCentrale.getFDocenteteCollection().add(FDocentete);
                CTNumCentrale = em.merge(CTNumCentrale);
            }
            if (cbDENo != null) {
                cbDENo.getFDocenteteCollection().add(FDocentete);
                cbDENo = em.merge(cbDENo);
            }
            if (cbLINo != null) {
                cbLINo.getFDocenteteCollection().add(FDocentete);
                cbLINo = em.merge(cbLINo);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findFDocentete(FDocentete.getCbMarq()) != null) {
                throw new PreexistingEntityException("FDocentete " + FDocentete + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(FDocentete FDocentete) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            FDocentete persistentFDocentete = em.find(FDocentete.class, FDocentete.getCbMarq());
            FCaisse cbCANoOld = persistentFDocentete.getCbCANo();
            FCaisse cbCANoNew = FDocentete.getCbCANo();
            FCollaborateur cbCONoCaissierOld = persistentFDocentete.getCbCONoCaissier();
            FCollaborateur cbCONoCaissierNew = FDocentete.getCbCONoCaissier();
            FCollaborateur cbCONoOld = persistentFDocentete.getCbCONo();
            FCollaborateur cbCONoNew = FDocentete.getCbCONo();
            FCompteg CGNumOld = persistentFDocentete.getCGNum();
            FCompteg CGNumNew = FDocentete.getCGNum();
            FComptet CTNumPayeurOld = persistentFDocentete.getCTNumPayeur();
            FComptet CTNumPayeurNew = FDocentete.getCTNumPayeur();
            FComptet CTNumCentraleOld = persistentFDocentete.getCTNumCentrale();
            FComptet CTNumCentraleNew = FDocentete.getCTNumCentrale();
            FDepot cbDENoOld = persistentFDocentete.getCbDENo();
            FDepot cbDENoNew = FDocentete.getCbDENo();
            FLivraison cbLINoOld = persistentFDocentete.getCbLINo();
            FLivraison cbLINoNew = FDocentete.getCbLINo();
            if (cbCANoNew != null) {
                cbCANoNew = em.getReference(cbCANoNew.getClass(), cbCANoNew.getCbMarq());
                FDocentete.setCbCANo(cbCANoNew);
            }
            if (cbCONoCaissierNew != null) {
                cbCONoCaissierNew = em.getReference(cbCONoCaissierNew.getClass(), cbCONoCaissierNew.getCbMarq());
                FDocentete.setCbCONoCaissier(cbCONoCaissierNew);
            }
            if (cbCONoNew != null) {
                cbCONoNew = em.getReference(cbCONoNew.getClass(), cbCONoNew.getCbMarq());
                FDocentete.setCbCONo(cbCONoNew);
            }
            if (CGNumNew != null) {
                CGNumNew = em.getReference(CGNumNew.getClass(), CGNumNew.getCbMarq());
                FDocentete.setCGNum(CGNumNew);
            }
            if (CTNumPayeurNew != null) {
                CTNumPayeurNew = em.getReference(CTNumPayeurNew.getClass(), CTNumPayeurNew.getCbMarq());
                FDocentete.setCTNumPayeur(CTNumPayeurNew);
            }
            if (CTNumCentraleNew != null) {
                CTNumCentraleNew = em.getReference(CTNumCentraleNew.getClass(), CTNumCentraleNew.getCbMarq());
                FDocentete.setCTNumCentrale(CTNumCentraleNew);
            }
            if (cbDENoNew != null) {
                cbDENoNew = em.getReference(cbDENoNew.getClass(), cbDENoNew.getCbMarq());
                FDocentete.setCbDENo(cbDENoNew);
            }
            if (cbLINoNew != null) {
                cbLINoNew = em.getReference(cbLINoNew.getClass(), cbLINoNew.getCbMarq());
                FDocentete.setCbLINo(cbLINoNew);
            }
            FDocentete = em.merge(FDocentete);
            if (cbCANoOld != null && !cbCANoOld.equals(cbCANoNew)) {
                cbCANoOld.getFDocenteteCollection().remove(FDocentete);
                cbCANoOld = em.merge(cbCANoOld);
            }
            if (cbCANoNew != null && !cbCANoNew.equals(cbCANoOld)) {
                cbCANoNew.getFDocenteteCollection().add(FDocentete);
                cbCANoNew = em.merge(cbCANoNew);
            }
            if (cbCONoCaissierOld != null && !cbCONoCaissierOld.equals(cbCONoCaissierNew)) {
                cbCONoCaissierOld.getFDocenteteCollection().remove(FDocentete);
                cbCONoCaissierOld = em.merge(cbCONoCaissierOld);
            }
            if (cbCONoCaissierNew != null && !cbCONoCaissierNew.equals(cbCONoCaissierOld)) {
                cbCONoCaissierNew.getFDocenteteCollection().add(FDocentete);
                cbCONoCaissierNew = em.merge(cbCONoCaissierNew);
            }
            if (cbCONoOld != null && !cbCONoOld.equals(cbCONoNew)) {
                cbCONoOld.getFDocenteteCollection().remove(FDocentete);
                cbCONoOld = em.merge(cbCONoOld);
            }
            if (cbCONoNew != null && !cbCONoNew.equals(cbCONoOld)) {
                cbCONoNew.getFDocenteteCollection().add(FDocentete);
                cbCONoNew = em.merge(cbCONoNew);
            }
            if (CGNumOld != null && !CGNumOld.equals(CGNumNew)) {
                CGNumOld.getFDocenteteCollection().remove(FDocentete);
                CGNumOld = em.merge(CGNumOld);
            }
            if (CGNumNew != null && !CGNumNew.equals(CGNumOld)) {
                CGNumNew.getFDocenteteCollection().add(FDocentete);
                CGNumNew = em.merge(CGNumNew);
            }
            if (CTNumPayeurOld != null && !CTNumPayeurOld.equals(CTNumPayeurNew)) {
                CTNumPayeurOld.getFDocenteteCollection().remove(FDocentete);
                CTNumPayeurOld = em.merge(CTNumPayeurOld);
            }
            if (CTNumPayeurNew != null && !CTNumPayeurNew.equals(CTNumPayeurOld)) {
                CTNumPayeurNew.getFDocenteteCollection().add(FDocentete);
                CTNumPayeurNew = em.merge(CTNumPayeurNew);
            }
            if (CTNumCentraleOld != null && !CTNumCentraleOld.equals(CTNumCentraleNew)) {
                CTNumCentraleOld.getFDocenteteCollection().remove(FDocentete);
                CTNumCentraleOld = em.merge(CTNumCentraleOld);
            }
            if (CTNumCentraleNew != null && !CTNumCentraleNew.equals(CTNumCentraleOld)) {
                CTNumCentraleNew.getFDocenteteCollection().add(FDocentete);
                CTNumCentraleNew = em.merge(CTNumCentraleNew);
            }
            if (cbDENoOld != null && !cbDENoOld.equals(cbDENoNew)) {
                cbDENoOld.getFDocenteteCollection().remove(FDocentete);
                cbDENoOld = em.merge(cbDENoOld);
            }
            if (cbDENoNew != null && !cbDENoNew.equals(cbDENoOld)) {
                cbDENoNew.getFDocenteteCollection().add(FDocentete);
                cbDENoNew = em.merge(cbDENoNew);
            }
            if (cbLINoOld != null && !cbLINoOld.equals(cbLINoNew)) {
                cbLINoOld.getFDocenteteCollection().remove(FDocentete);
                cbLINoOld = em.merge(cbLINoOld);
            }
            if (cbLINoNew != null && !cbLINoNew.equals(cbLINoOld)) {
                cbLINoNew.getFDocenteteCollection().add(FDocentete);
                cbLINoNew = em.merge(cbLINoNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = FDocentete.getCbMarq();
                if (findFDocentete(id) == null) {
                    throw new NonexistentEntityException("The fDocentete with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            FDocentete FDocentete;
            try {
                FDocentete = em.getReference(FDocentete.class, id);
                FDocentete.getCbMarq();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The FDocentete with id " + id + " no longer exists.", enfe);
            }
            FCaisse cbCANo = FDocentete.getCbCANo();
            if (cbCANo != null) {
                cbCANo.getFDocenteteCollection().remove(FDocentete);
                cbCANo = em.merge(cbCANo);
            }
            FCollaborateur cbCONoCaissier = FDocentete.getCbCONoCaissier();
            if (cbCONoCaissier != null) {
                cbCONoCaissier.getFDocenteteCollection().remove(FDocentete);
                cbCONoCaissier = em.merge(cbCONoCaissier);
            }
            FCollaborateur cbCONo = FDocentete.getCbCONo();
            if (cbCONo != null) {
                cbCONo.getFDocenteteCollection().remove(FDocentete);
                cbCONo = em.merge(cbCONo);
            }
            FCompteg CGNum = FDocentete.getCGNum();
            if (CGNum != null) {
                CGNum.getFDocenteteCollection().remove(FDocentete);
                CGNum = em.merge(CGNum);
            }
            FComptet CTNumPayeur = FDocentete.getCTNumPayeur();
            if (CTNumPayeur != null) {
                CTNumPayeur.getFDocenteteCollection().remove(FDocentete);
                CTNumPayeur = em.merge(CTNumPayeur);
            }
            FComptet CTNumCentrale = FDocentete.getCTNumCentrale();
            if (CTNumCentrale != null) {
                CTNumCentrale.getFDocenteteCollection().remove(FDocentete);
                CTNumCentrale = em.merge(CTNumCentrale);
            }
            FDepot cbDENo = FDocentete.getCbDENo();
            if (cbDENo != null) {
                cbDENo.getFDocenteteCollection().remove(FDocentete);
                cbDENo = em.merge(cbDENo);
            }
            FLivraison cbLINo = FDocentete.getCbLINo();
            if (cbLINo != null) {
                cbLINo.getFDocenteteCollection().remove(FDocentete);
                cbLINo = em.merge(cbLINo);
            }
            em.remove(FDocentete);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<FDocentete> findFDocenteteEntities() {
        return findFDocenteteEntities(true, -1, -1);
    }

    public List<FDocentete> findFDocenteteEntities(int maxResults, int firstResult) {
        return findFDocenteteEntities(false, maxResults, firstResult);
    }

    private List<FDocentete> findFDocenteteEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(FDocentete.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public FDocentete findFDocentete(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(FDocentete.class, id);
        } finally {
            em.close();
        }
    }

    public int getFDocenteteCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<FDocentete> rt = cq.from(FDocentete.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
